﻿using Minotaur.App.Core.Contrls;
using Minotaur.App.Domains.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Minotaur.App.Modules.SystemControl.Views
{
    /// <summary>
    /// SideExpander.xaml 的交互逻辑
    /// </summary>
    public partial class SideExpander : UserControl
    {
        public SideExpander()
        {
            InitializeComponent();
        }
        public SideExpander(MenuInfoDto menuInfo)
        {
            MenuInfo = menuInfo;
            InitializeComponent();
            this.DataContext = menuInfo;
            InitItems();
        }

        private void InitItems()
        {
            if (this.MenuInfo != null && this.MenuInfo.Childs != null && this.MenuInfo.Childs.Any())
            {
                foreach (var item in this.MenuInfo.Childs)
                {
                    if (item.Childs == null || !item.Childs.Any())
                    {
                        var ite = new SideRadioButton(item);
                        ite.OpendMenuClick += OpendMenuClicked;
                        ite.Margin = new Thickness(20, 0, 0, 0);
                        this.ExpanderItem.Children.Add(ite);
                    }
                    else
                    {
                        var ite = new SideExpander(item);
                        ite.Margin = new Thickness(20, 0, 0, 0);
                        this.ExpanderItem.Children.Add(ite);
                    }
                }
            }
        }

        private void OpendMenuClicked(object sender, RoutedEventArgs e)
        {
            MenuInfoDto infoDto = e.OriginalSource as MenuInfoDto;
            RoutedEventArgs args = new RoutedEventArgs(OpendMenuEvent, infoDto);
            RaiseEvent(args);
        }

        private MenuInfoDto _MenuInfo;

        public MenuInfoDto MenuInfo { get => _MenuInfo; set => _MenuInfo = value; }

        //注册打开菜单节点配置事件
        public static readonly RoutedEvent OpendMenuEvent =
              EventManager.RegisterRoutedEvent(ClickNameConst.OpendMenuClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SideExpander));

        /// <summary>
        /// 跳转菜单对应路由页面
        /// </summary>
        public event RoutedEventHandler OpendMenuClick
        {
            add { AddHandler(OpendMenuEvent, value); }
            remove { RemoveHandler(OpendMenuEvent, value); }
        }
    }
}
